package com.aibili.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.AdminInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class AdminInfoService extends BaseService {

	/**
	 * 
	 * @Description 查询后台用户信息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<AdminInfo> getByParams(Integer pageIndex, Integer pageSize) {
		String chql = " select count(b) from AdminInfo b ";
		String hql = " from AdminInfo ";

		PageBodyModel<AdminInfo> result = new PageBodyModel<AdminInfo>();
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<AdminInfo> banners = new ArrayList<AdminInfo>();
			banners = (List<AdminInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			result = new PageBodyModel<>(page, banners);
		}
		return result;
	}

	/**
	 * 
	 * @Description 通过 id 获取后台用户信息
	 * @param id
	 * @return
	 */
	public AdminInfo getAdminById(long id) {
		AdminInfo admin = dataDao.getObjectById(AdminInfo.class, id);
		return admin;
	}

	/**
	 * 
	 * @Description 修改后台用户
	 * @param admin
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateAdmin(AdminInfo admin) {
		dataDao.updateObject(admin);
	}

	/**
	 * 
	 * @Description 新增后台用户
	 * @param admin
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addAdmin(AdminInfo admin) {
		dataDao.addObject(admin);
	}

	/**
	 * 
	 * @Description 通过 user_name 查找后台用户信息
	 * @param user_name
	 * @return
	 */
	public AdminInfo getAdminByName(String user_name) {
		String hql = " from AdminInfo where user_name = :user_name";
		AdminInfo admin = (AdminInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "user_name" }, user_name);
		return admin;
	}

	/**
	 * 
	 * @Description 通过 user_name 查找非该 id 用户的信息
	 * @param user_name
	 * @param id
	 * @return
	 */
	public AdminInfo getAdminByNameId(String user_name, long id) {
		String hql = " from AdminInfo where user_name = :user_name and id != :id";
		AdminInfo admin = (AdminInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "user_name", "id" }, user_name,
				id);
		return admin;
	}

	/**
	 * 
	 * @Description 通过 id 删除后台用户信息
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteAdminById(long id) {
		AdminInfo admin = dataDao.getObjectById(AdminInfo.class, id);
		if (admin != null) {
			dataDao.deleteObject(admin);
		}
	}

	/**
	 * 
	 * @Description 获取所有的后台用户
	 * @return
	 */
	public List<AdminInfo> getAllAdmin() {
		List<AdminInfo> admins = new ArrayList<>();
		admins = dataDao.getAllObject(AdminInfo.class);
		return admins;
	}

}
